|
1
|
|
|
// This file is part of the API Platform project. |
|
2
|
|
|
// |
|
3
|
|
|
// (c) Kévin Dunglas <[email protected]> |
|
4
|
|
|
// |
|
5
|
|
|
// For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
// file that was distributed with this source code. |
|
7
|
|
|
|
|
8
|
|
|
function loadSwaggerUI(userOptions = {}) { |
|
9
|
|
|
const data = JSON.parse(document.getElementById('swagger-data').innerText); |
|
10
|
|
|
const defaultOptions = { |
|
11
|
|
|
spec: data.spec, |
|
12
|
|
|
dom_id: '#swagger-ui', |
|
13
|
|
|
validatorUrl: null, |
|
14
|
|
|
presets: [ |
|
15
|
|
|
SwaggerUIBundle.presets.apis, |
|
|
|
|
|
|
16
|
|
|
SwaggerUIStandalonePreset |
|
|
|
|
|
|
17
|
|
|
], |
|
18
|
|
|
plugins: [ |
|
19
|
|
|
SwaggerUIBundle.plugins.DownloadUrl |
|
20
|
|
|
], |
|
21
|
|
|
layout: 'StandaloneLayout' |
|
22
|
|
|
}; |
|
23
|
|
|
const options = Object.assign({}, defaultOptions, userOptions); |
|
24
|
|
|
const ui = SwaggerUIBundle(options); |
|
25
|
|
|
|
|
26
|
|
|
const storageKey = 'nelmio_api_auth'; |
|
27
|
|
|
|
|
28
|
|
|
// if we have auth in storage use it |
|
29
|
|
|
if (sessionStorage.getItem(storageKey)) { |
|
|
|
|
|
|
30
|
|
|
try { |
|
31
|
|
|
ui.authActions.authorize(JSON.parse(sessionStorage.getItem(storageKey))); |
|
32
|
|
|
} catch (ignored) { |
|
|
|
|
|
|
33
|
|
|
// catch any errors here so it does not stop script execution |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// hook into authorize to store the auth in local storage when user performs authorization |
|
38
|
|
|
const currentAuthorize = ui.authActions.authorize; |
|
39
|
|
|
ui.authActions.authorize = function (payload) { |
|
40
|
|
|
sessionStorage.setItem(storageKey, JSON.stringify(payload)); |
|
|
|
|
|
|
41
|
|
|
return currentAuthorize(payload); |
|
42
|
|
|
}; |
|
43
|
|
|
|
|
44
|
|
|
// hook into logout to clear auth from storage if user logs out |
|
45
|
|
|
const currentLogout = ui.authActions.logout; |
|
46
|
|
|
ui.authActions.logout = function (payload) { |
|
47
|
|
|
sessionStorage.removeItem(storageKey); |
|
|
|
|
|
|
48
|
|
|
return currentLogout(payload); |
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
|
|
window.ui = ui; |
|
52
|
|
|
} |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.